package srzy.module

import srzy.data.CaseInfo
import srzy.common.ShellExecute
import java.io.File
import srzy.common._
import srzy.data.PublicData

object CaseDeal extends srzy.common.Logging {

  var l = List[AbsCaseDeal](CleanDir, ShellInit, WriteInputFile, UpdateTaskId, ReadCdrFile, CdrComp, DBRec)
  if (Config.getValue("root", "config", "DBCompareFlag").toBoolean == true)
    l = List[AbsCaseDeal](CleanDir, ShellInit, WriteInputFile, UpdateTaskId, ReadCdrFile, CdrComp, DBComp) //, FileRec

  //  val l = List[AbsCaseDeal](DBComp)
  def getCaseInfo(caseId: Int) = {
    PublicData.caseInfos.apply(caseId)
  }

  def dealAllCase = {
    logger.debug("Begin to deal")
    logger.debug("caseNames:" + PublicData.caseInfos.map(x => x.caseName).mkString(" "))
    val dbRecFlag: Boolean = if (Config.getValue("root", "config", "CaseNameFile") == "") true else false
    logger.debug("dbRecFlag:" + dbRecFlag)
    for (i <- 0 until PublicData.caseInfos.length) {
      try {
        for (e <- l) {
          val className = ("""[\w]+""".r findAllIn e.getClass().getName()).toList.last
          logger.debug(className + " begin deal " + (i + 1) + ":" + getCaseInfo(i).caseName)
          e.setCaseId(i)
          e.deal
          logger.debug(className + " deal over " + (i + 1) + ":" + getCaseInfo(i).caseName)
        }
      } catch {
        case ex: Exception =>
          {
            logger.error(ex.getMessage(), ex)
            getCaseInfo(i).caseResult = false
          }
      }
      logger.debug("FileRec begin deal " + (i + 1) + ":" + getCaseInfo(i).caseName)
      FileRec.setCaseId(i)
      FileRec.deal
      logger.debug("FileRec deal over " + (i + 1) + ":" + getCaseInfo(i).caseName)

      if (dbRecFlag == true) {
        logger.debug("DBRec begin deal " + (i + 1) + ":" + getCaseInfo(i).caseName)
        DBRec.setCaseId(i)
        DBRec.deal
        logger.debug("DBRec deal over " + (i + 1) + ":" + getCaseInfo(i).caseName)
      }

    }
    logger.debug("Deal case over")

  }
}



